//
//  HomeTopicCollectionCell.m
//  jingdongfang
//
//  Created by LJL on 2018/5/10.
//  Copyright © 2018年 BOE. All rights reserved.
//

#import "HomeTopicCollectionCell.h"
#import "HomeTopicCell.h"
#import "IGHomeDailyActivityApi.h"
#import "IGCommunityDetailVC.h"
@interface HomeTopicCollectionCell ()<UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout>

@end

@implementation HomeTopicCollectionCell
- (void)layoutSubviews
{
    [super layoutSubviews];
    [self addSubview:self.collectionView];
}

- (UICollectionView *)collectionView
{
    if (!_collectionView)
    {
        UICollectionViewFlowLayout *flow = [[UICollectionViewFlowLayout alloc] init];
        [flow setScrollDirection:UICollectionViewScrollDirectionHorizontal];
        self.collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0,  KSCREENWIDTH,KSCREENWIDTH  * 202 / 750 + 80) collectionViewLayout:flow];
        self.collectionView.backgroundColor = [UIColor c3Color];
        self.collectionView.delegate = self;
        self.collectionView.dataSource = self;
        self.collectionView.showsVerticalScrollIndicator = YES;//垂直方向
        self.collectionView.showsHorizontalScrollIndicator = NO;//水平方向
        [self.collectionView registerNib:[UINib nibWithNibName:@"HomeTopicCell" bundle:nil] forCellWithReuseIdentifier:@"HomeTopicCell"];
    }
    return _collectionView;
}

- (void)setDataArr:(NSMutableArray *)dataArr
{
    if (_dataArr != dataArr)
    {
        _dataArr = dataArr;
        [self.collectionView reloadData];
    }
}
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return 1;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
        return self.dataArr.count > 0 ? self.dataArr.count : 0;
//    return 10;
}

//设置item大小
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    return CGSizeMake(KSCREENWIDTH * 406 / 750, KSCREENWIDTH  * 202 / 750 + 61);
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    HomeTopicCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"HomeTopicCell" forIndexPath:indexPath];
    IGHomeCircles *clrModel = _dataArr[indexPath.row];
    [cell.imageV sd_setImageWithURL:[NSURL URLWithString:clrModel.image]];
    cell.titleL.text = clrModel.title;
    cell.subTitleL.text = clrModel.content;
    return cell;
}

//设置分区边界
- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
{
    return UIEdgeInsetsMake(0, 6, 19, 16);
}

#pragma mark --- item 点击事件
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    IGHomeCircles *cir = self.dataArr[indexPath.row];
    IGCommunityDetailVC *vc = [[IGCommunityDetailVC alloc] initWithCid:cir.cid];
    [UIViewController ig_pushViewController:vc animated:YES];
}

@end
